//
//  YGPrescriptionViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/30.
//

#import "YGPrescriptionViewController.h"
#import "YGChuFangTableViewCell.h"
#import "YGPrescriptionBLL.h"
#import "YGPreHeaderView.h"
@interface YGPrescriptionViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIView  *topView;
@property (nonatomic, strong) UIView  *contentView;
@property (nonatomic, strong) UIView  *bottomView;

@property (nonatomic, strong) UILabel  *sickLabel;
@property (nonatomic, strong) UILabel  *bmiLabel;

@property (nonatomic, strong) UITableView  *tableView;

@property (nonatomic, strong) UIView  *headerView;
@property (nonatomic, strong) UILabel  *drRecommandLabel;
@property (nonatomic, strong) UIButton  *priceSelectBtn;

@property (nonatomic, strong) YGPreHeaderView  *sectionView;


@property (nonatomic, strong) UIButton  *selectBtn;
@property (nonatomic, strong) UILabel  *priceLabel;
@property (nonatomic, strong) UIButton  *carBtn;
@property (nonatomic, strong) UIButton  *bugBtn;


@property (nonatomic, strong) NSArray  *dataArray;
@property (nonatomic, strong) YGPrescriptionModel  *model;
@property (nonatomic, strong) YGParamesModel  *lastModel;
@property (nonatomic, assign) NSInteger  priceSort;

@end

@implementation YGPrescriptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"处方结果";
    [self initView];
    
    [self initMas];
    
    [self requestData];
    
    
    // Do any additional setup after loading the view.
}

- (void)initView{
    [self.view addSubview:self.topView];
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.contentView];
    [self.view addSubview:self.bottomView];
    
    [self.topView addSubview:self.sickLabel];
    [self.topView addSubview:self.bmiLabel];
    
    [self.contentView addSubview:self.tableView];
    
    [self.bottomView addSubview:self.selectBtn];
    [self.bottomView addSubview:self.priceLabel];
    [self.bottomView addSubview:self.carBtn];
    [self.bottomView addSubview:self.bugBtn];
    
    [self.headerView addSubview:self.drRecommandLabel];
    [self.headerView addSubview:self.priceSelectBtn];
}

- (void)initMas{
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kNavigationHeight);
        make.height.mas_equalTo(50);
    }];
    
    [self.sickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(self.topView.mas_height);
        make.left.mas_equalTo(10);
    }];
    
    [self.bmiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.topView.mas_width).multipliedBy(0.5);
        make.left.mas_equalTo(self.sickLabel.mas_right);
        make.height.mas_equalTo(self.sickLabel);
    }];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.topView.mas_bottom);
        make.height.mas_equalTo(60);
    }];
    
    [self.drRecommandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(22);
        make.height.mas_equalTo(22);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(115);
    }];
    
    [self.priceSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(26);
        make.width.mas_equalTo(95);
        make.centerY.mas_equalTo(self.drRecommandLabel.mas_centerY);
        make.right.mas_equalTo(self.view).offset(-10);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
   
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        make.height.mas_equalTo(82);
    }];
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(60);
        make.left.mas_equalTo(10);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(10);
        make.height.mas_equalTo(self.selectBtn.mas_height);
        make.width.mas_equalTo(100);
    }];
    CGFloat left =  self.view.width/9.0;
    [self.carBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.bottomView.mas_width).multipliedBy(1/3.0);
        make.height.mas_equalTo(36);
        make.top.mas_equalTo(self.selectBtn.mas_bottom).offset(6);
        make.left.mas_equalTo(self.bottomView).offset(left);
    }];
    
    [self.bugBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(36);
        make.top.mas_equalTo(self.selectBtn.mas_bottom).offset(6);
        make.left.mas_equalTo(self.carBtn.mas_right).offset(left);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(1/3.0);

    }];
}

- (void)requestData{
    WeakSelf(self);
    [YGPrescriptionBLL requestPrescriptionWith:self.pid priceSord:self.priceSort Success:^(YGPrescriptionModel * _Nonnull model) {
        weakself.model = model;
        YGParamesModel *yg = [model.parames firstObject];
     
        weakself.dataArray = [yg goodsInfo];
        weakself.sickLabel.text = model.diseaseName;
        weakself.bmiLabel.text = [NSString stringWithFormat:@"BMI指数:%ld",(long)model.bmi];
        [weakself.tableView reloadData];
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)clickCarAction{
    
}

- (void)clickBugAction{
    
}

- (void)clickPriceSelectAction{
    self.priceSelectBtn.selected = !self.priceSelectBtn.selected;
    
    if (self.priceSelectBtn) {
        self.priceSort = 1;
    }else{
        self.priceSort = 2;
    }
    [self requestData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.model.parames.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    YGParamesModel *model = [self.model.parames objectAtIndex:section];
    if (model.isShow) {
        return model.goodsInfo.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.sectionView = [[YGPreHeaderView alloc] init];
    WeakSelf(self);
    self.sectionView.ClickBlock = ^(NSInteger tag) {
        YGParamesModel *model  = [weakself.model.parames objectAtIndex:tag];
        if (weakself.lastModel) {
            weakself.lastModel.isShow = NO;
        }
        model.isShow = YES;
        [weakself.tableView reloadData];
        weakself.lastModel = model;
    };
    self.sectionView.tag = section;
    return self.sectionView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YGChuFangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(YGChuFangTableViewCell.class)];
    if (!cell) {
        cell = [[YGChuFangTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(YGChuFangTableViewCell.class)];
    }
    [cell configureData:self.dataArray[indexPath.row]];
    return cell;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] init];
    }
    return _topView;
}

- (UIView *)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}

- (UIView *)bottomView{
    if(!_bottomView){
        _bottomView = [[UIView alloc] init];
    }
    return _bottomView;
}


- (UILabel *)sickLabel
{
    if(!_sickLabel){
        _sickLabel = [[UILabel alloc] init];
    }
    return _sickLabel;
}

- (UILabel *)bmiLabel
{
    if(!_bmiLabel){
        _bmiLabel = [[UILabel alloc] init];
    }
    return _bmiLabel;
}

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_tableView registerClass:YGChuFangTableViewCell.class forCellReuseIdentifier:NSStringFromClass(YGChuFangTableViewCell.class)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UIView *)headerView
{
    if(!_headerView){
        _headerView = [[UIView alloc] init];
    }
    return _headerView;
}

- (UIButton *)selectBtn
{
    if(!_selectBtn){
        _selectBtn = [[UIButton alloc] init];
    }
    return _selectBtn;
}

- (UILabel *)priceLabel
{
    if(!_priceLabel){
        _priceLabel = [[UILabel alloc] init];
    }
    return _priceLabel;
}


- (UIButton *)carBtn
{
    if(!_carBtn){
        _carBtn = [[UIButton alloc] init];
        _carBtn.backgroundColor = UIColorFromRGB(0xFFC13F);
        [_carBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_carBtn addTarget:self action:@selector(clickCarAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _carBtn;
}

- (UIButton *)bugBtn
{
    if(!_bugBtn){
        _bugBtn = [[UIButton alloc] init];
        _bugBtn.backgroundColor = UIColorFromRGB(0xE55667);
        [_bugBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        [_bugBtn addTarget:self action:@selector(clickBugAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bugBtn;
}

- (UILabel *)drRecommandLabel
{
    if(!_drRecommandLabel){
        _drRecommandLabel = [[UILabel alloc] init];
        _drRecommandLabel.backgroundColor = UIColorFromRGB(0x37C8D7);
        _drRecommandLabel.font = [UIFont systemFontOfSize:12];
        _drRecommandLabel.textAlignment = NSTextAlignmentCenter;
        _drRecommandLabel.text = @"医生优先建议处方";
    }
    return _drRecommandLabel;
}

- (UIButton *)priceSelectBtn
{
    if(!_priceSelectBtn){
        _priceSelectBtn = [[UIButton alloc] init];
        [_priceSelectBtn setTitle:@"价格从低到高" forState:UIControlStateNormal];
        [_priceSelectBtn setTitle:@"价格从高到低" forState:UIControlStateSelected];
        [_priceSelectBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        [_priceSelectBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateSelected];
        _priceSelectBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_priceSelectBtn addTarget:self action:@selector(clickPriceSelectAction) forControlEvents:UIControlEventTouchUpInside];
        _priceSelectBtn.layer.borderWidth = 0.5;
    }
    return _priceSelectBtn;
}



@end
